#include "Tex_Bitmap.h"
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//	Constructors and Deconstructors
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//==================================================================================================
//	Blank Constructor
//==================================================================================================
BGL_Bitmap::BGL_Bitmap()
{
	m_id = 0;
}
//==================================================================================================
//	Deconstructor
//==================================================================================================
BGL_Bitmap::~BGL_Bitmap()
{
	if(glIsTexture(m_id))
		glDeleteTextures(1, &m_id);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//		Bitmap Functions
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//==================================================================================================
//	Basic Loader for Bitmap Image
//==================================================================================================
void BGL_Bitmap::Load(const char* _filename)
{
	//----------------------------------------
	//	Init
	//	Notice the loop to find next texture
	//	name/id avalible
	//----------------------------------------
	m_id = 0;
	while(glIsTexture(m_id))	
	{
		++m_id;
	}
	AUX_RGBImageRec _texture;
	memset(&_texture,0,sizeof(_texture));
	//---------------------------------------------------
	//	Check File exsists and can be opened
	//---------------------------------------------------
	FILE *_fileHandle = 0;
	if (_filename)
		_fileHandle = fopen(_filename,"r");

	//---------------------------------
	//	Load Image
	//	auxDIBImageLoad does the work
	//---------------------------------
	if (_fileHandle)
	{
		fclose(_fileHandle);
		_texture = *(auxDIBImageLoad(_filename));
	}

	//-------------------------------------
	//	Create the Texture
	//-------------------------------------
	if(_texture.data)
	{
		glGenTextures(1, &m_id);

		//-------------------------------------
		//	Generate Info and Maps
		//	Note this binds the texture
		//-------------------------------------
		glBindTexture(GL_TEXTURE_2D, m_id);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, _texture.sizeX, _texture.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, _texture.data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

		//-----------------------------------------------
		//	Clean up Texture
		//-----------------------------------------------
		free(_texture.data);
	}
}
//==================================================================================================
//	Bind Texture
//==================================================================================================
void BGL_Bitmap::BindTexture()
{
	glBindTexture(GL_TEXTURE_2D, m_id);
}
